package com.sangmo.fts.api.clazz;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.clazz.model.Clazz;
import com.sangmo.fts.clazz.service.ClazzService;
import com.sangmo.fts.user.model.User;
import com.sangmo.fts.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Collection;
import java.util.HashMap;

@Controller
@RequestMapping("/clazz/v1")
@Access("clazz")
public class ClazzApi {

    @Autowired
    private UserService userService;

    @Autowired
    private ClazzService clazzService;

    @Access(":member:read")
    @PostMapping("/member/search")
    public @ResponseBody
    ApiResponse<PageResult<User>> search(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(userService.clazzMemberSearch(ps));
    }

    @Access(":selection")
    @PostMapping("/selection")
    public @ResponseBody
    ApiResponse<PageResult<Clazz>> selection(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(clazzService.selection(ps));
    }

    @Access(":selection")
    @PostMapping("/examination/{id}/selection")
    public @ResponseBody
    ApiResponse<Collection<Clazz>> examinationSelection(@PathVariable("id") Long id) {
        return ApiResponse.make(clazzService.findByExaminationId(id));
    }


}
